/**
 * 
 */
package cn.kuaipai.weibo.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import cn.kuaipai.utils.BeanUtils;
import cn.kuaipai.utils.StringTools;
import cn.kuaipai.weibo.constants.WeiboConstants;
import cn.kuaipai.weibo.utils.APIURL;


/**
 * 微博授权认证信息
 * @author zy
 */
@Component
public class OAuth implements Serializable {
	private static final long serialVersionUID = 4394299392686394745L;

	/**
	 * App Key(应用信息中的App Key值)
	 */
	private String oauthConsumerKey = WeiboConstants.APP_KEY ;
	/**
	 * App Secret(应用信息中的App Secret值)
	 */
	private String oauthConsumerSecret = WeiboConstants.APP_SECRET ;
	/**
	 * 签名方法，暂只支持HMAC-SHA1
	 */
	private String oauthSignatureMethod = "HMAC-SHA1";
	/**
	 * 签名值，密钥为：App Secret,需要生成。
	 */
	private String oauthSignature = "" ;
	/**
	 * 时间戳, 其值是距1970 00:00:00 GMT的秒数，必须是大于0的整数(10位)
	 */
	private String oauthTimestamp = "" ;
	/**
	 * 单次值，随机生成的32位字符串，防止重放攻击（每次请求必须不同）--临时码
	 */
	private String oauthNonce = "";
	/**
	 * 认证成功后浏览器会被重定向到这个url中,获取对应的oauth_verifier
	 */
	private String oauthCallback = "";
	/**
	 * 版本号
	 */
	private String oauthVersion = "1.0";
	/**
	 * 通指两种：
	 * 第一种是交换requestToken时候返回oauth_token,oauth_token_secrect,
	 * 第二种是交换accessToken时候返回access_token,access_token_secrect
	 */
	private String oauthToken ;
	private String oauthTokenSecret ;
	/**
	 * 授权验证码
	 */
	private String oauthVerifier ;
	
	/**
	 * oauth授权完成后返回对应的openid
	 */
	private String openid ;
	/**
	 * oauth授权完成后返回对应的openkey
	 */
	private String openkey ;
	
	private int oauthStatus = WeiboConstants.RESULTCODE_OAUTHSTATUS_SUCCESS ;
	
	public OAuth(){
	}
	public String getOauthConsumerKey() {
		return oauthConsumerKey;
	}
	public void setOauthConsumerKey(String oauthConsumerKey) {
		this.oauthConsumerKey = oauthConsumerKey;
	}
	public String getOauthConsumerSecret() {
		return oauthConsumerSecret;
	}
	public void setOauthConsumerSecret(String oauthConsumerSecret) {
		this.oauthConsumerSecret = oauthConsumerSecret;
	}
	public String getOauthSignatureMethod() {
		return oauthSignatureMethod;
	}
	public void setOauthSignatureMethod(String oauthSignatureMethod) {
		this.oauthSignatureMethod = oauthSignatureMethod;
	}
	public String getOauthSignature() {
		return oauthSignature;
	}
	public void setOauthSignature(String oauthSignature) {
		this.oauthSignature = oauthSignature;
	}
	public String getOauthTimestamp() {
		return oauthTimestamp;
	}
	public void setOauthTimestamp(String oauthTimestamp) {
		this.oauthTimestamp = oauthTimestamp;
	}
	public String getOauthNonce() {
		return oauthNonce;
	}
	public void setOauthNonce(String oauthNonce) {
		this.oauthNonce = oauthNonce;
	}
	public String getOauthCallback() {
		return oauthCallback;
	}
	public void setOauthCallback(String oauthCallback) {
		this.oauthCallback = oauthCallback;
	}
	public String getOauthVersion() {
		return oauthVersion;
	}
	public void setOauthVersion(String oauthVersion) {
		this.oauthVersion = oauthVersion;
	}
	public String getOauthToken() {
		return oauthToken;
	}
	public void setOauthToken(String oauthToken) {
		this.oauthToken = oauthToken;
	}
	public String getOauthTokenSecret() {
		return oauthTokenSecret;
	}
	public void setOauthTokenSecret(String oauthTokenSecret) {
		this.oauthTokenSecret = oauthTokenSecret;
	}
	public int getOauthStatus() {
		return oauthStatus;
	}
	public void setOauthStatus(int oauthStatus) {
		this.oauthStatus = oauthStatus;
	}
	
	public String getOauthVerifier() {
		return oauthVerifier;
	}
	public void setOauthVerifier(String oauthVerifier) {
		this.oauthVerifier = oauthVerifier;
	}
	public String getOpenid() {
		return openid;
	}
	public void setOpenid(String openid) {
		this.openid = openid;
	}
	public String getOpenkey() {
		return openkey;
	}
	public void setOpenkey(String openkey) {
		this.openkey = openkey;
	}
	@Override
	public String toString() {
		return BeanUtils.reflectionToString(this);
	}
	/**
	 * 生成时间戳
	 * @return
	 */
	private String generateOauthTimeStamp() {
		return String.valueOf(System.currentTimeMillis() / 1000);
	}
	/**
	 * 生成单次值，随机生成的32位字符串
	 * @return
	 */
	private String generateOauthNonce() {
		return StringTools.getUUID();
	}
	
	/**
	 * 生成request Token请求参数(请求服务商取得oauth_token,oauth_token_scecret)
	 * 
	 * @return List<WeiboParameter>
	 */
	public List<WeiboParameter> getRequestTokenParameters() {
		List<WeiboParameter> parameters = new ArrayList<WeiboParameter>();
		oauthTimestamp = generateOauthTimeStamp().trim() ;
		oauthNonce = generateOauthNonce().trim() ;
		oauthCallback = APIURL.getCallbackURL().trim();
		
		if(StringUtils.isNotBlank(oauthConsumerKey))
			parameters.add(new WeiboParameter("oauth_consumer_key",oauthConsumerKey.trim()));
		
		if(StringUtils.isNotBlank(oauthSignatureMethod))
			parameters.add(new WeiboParameter("oauth_signature_method",oauthSignatureMethod));
		
		if(StringUtils.isNotBlank(oauthTimestamp))
			parameters.add(new WeiboParameter("oauth_timestamp",oauthTimestamp));
		
		if(StringUtils.isNotBlank(oauthNonce))
			parameters.add(new WeiboParameter("oauth_nonce",oauthNonce));
		
		if(StringUtils.isNotBlank(oauthCallback))
			parameters.add(new WeiboParameter("oauth_callback",oauthCallback));
		
		if(StringUtils.isNotBlank(oauthVersion))
			parameters.add(new WeiboParameter("oauth_version",oauthVersion.trim()));	
		
		return parameters;
	}
	/**
	 * 交换access token所需要的参数(请求服务商,取到真正有用的access_token,access_token_screct)
	 * 
	 * 添加了授权验证码
	 * @return
	 */
	public List<WeiboParameter> getAccessTokenParameters(){		
		List<WeiboParameter> parameters = this.getAccessParameters();
		if(StringUtils.isNotBlank(oauthVerifier))
			parameters.add(new WeiboParameter("oauth_verifier", oauthVerifier));
		return parameters;
	}
	/**
	 * 访问用户的所有信息所需参数
	 * 
	 * @return
	 */
	public List<WeiboParameter> getAccessParameters() {
		List<WeiboParameter> parameters = new ArrayList<WeiboParameter>();
		oauthTimestamp = generateOauthTimeStamp() ;
		oauthNonce = generateOauthNonce() ;
		parameters.add(new WeiboParameter("oauth_consumer_key", oauthConsumerKey));
		parameters.add(new WeiboParameter("oauth_signature_method", oauthSignatureMethod));
		parameters.add(new WeiboParameter("oauth_timestamp", oauthTimestamp));
		parameters.add(new WeiboParameter("oauth_nonce", oauthNonce));
		parameters.add(new WeiboParameter("oauth_token", oauthToken));
		parameters.add(new WeiboParameter("oauth_version", oauthVersion));		
		return parameters;
	}
	
	/**
	 * (openid)生成访问微博平台需要的参数
	 * @return
	 */
	public List<WeiboParameter> getOpenIdAccessParameters() {
		List<WeiboParameter> parameters = new ArrayList<WeiboParameter>();
		parameters.add(new WeiboParameter("appid", oauthConsumerKey));
//		parameters.add(new WeiboParameter("clientip", ""));  // 可选
		parameters.add(new WeiboParameter("reqtime", generateOauthTimeStamp()));
		parameters.add(new WeiboParameter("wbversion", "1"));		
		return parameters;
	}
	
	
	public static void main(String[] args) {
		OAuth oauth = new OAuth();
		System.out.println(oauth.getOpenIdAccessParameters());
	}
}
